package com.tg.app.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.core.util.Consumer;

import com.tange.core.backend.service.response.RxResponse;
import com.tange.core.data.structure.Ret;
import com.tange.core.device.manage.DeviceRename;
import com.tange.module.camera.hub.CameraHub;
import com.tange.module.device.info.DeviceInfoServiceManager;
import com.tg.app.R;
import com.base.BaseActivity;
import com.tg.app.activity.device.settings.DeviceInfoActivity;
import com.tg.data.bean.DeviceItem;
import com.tg.data.bean.DeviceItem_;
import com.appbase.custom.constant.CommonConstants;

import com.tg.app.util.ObjectBoxUtil;

import com.tg.data.http.entity.ChangeDeviceNameBean;

import io.objectbox.Box;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;


public class ChangeDeviceNameActivity extends BaseActivity implements View.OnClickListener {
    private EditText nameOne;
    private String device_id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//remove title bar  即隐藏标题栏
        setContentView(R.layout.activity_change_nick_name);
        hideActionBar();
        initView();
    }

    protected void initView(){
        RelativeLayout toolbar = findViewById(R.id.toolbar);
        TextView title = toolbar.findViewById(R.id.device_name);
        ImageButton back = toolbar.findViewById(R.id.back_toolbar);
        Intent  intent = getIntent();
        String device_name = intent.getStringExtra(DeviceInfoActivity.EXT_DEVICE_NAME);
        device_id =intent.getStringExtra(CommonConstants.EXT_DEVICE_ID);
        TextView save = toolbar.findViewById(R.id.toolbar_select);
         final ImageButton clear = findViewById(R.id.delete_edit_text);
        save.setOnClickListener(this);
        back.setOnClickListener(this);
        title.setText(R.string.change_device_name);
         nameOne = findViewById(R.id.change_nick_name_edit_text);
         if (!TextUtils.isEmpty(device_name)){
             nameOne.setText(device_name);
             clear.setVisibility(View.VISIBLE);
         }

        nameOne.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(!TextUtils.isEmpty(s)){
                    clear.setVisibility(View.VISIBLE);
                }else{
                    clear.setVisibility(View.GONE);
                }
            }
        });
        clear.setOnClickListener(v -> nameOne.setText(""));

    }

    @Override
    public boolean shouldSetStatusBarColor() {
        return true;
    }

    @Override

    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.toolbar_select) {
            setDeviceName();
        } else if (id == R.id.back_toolbar) {
            finish();
        }

    }
    private void setDeviceName(){
        final String name = nameOne.getText().toString().trim();

        if (TextUtils.isEmpty(name)){
            showToast(R.string.device_name_is_empty);
            return;
        }

        DeviceRename.INSTANCE.require(device_id, name, ret -> {
            if (ret.getSuccess()) {
                successChange(name);
                finish();
            } else {
                Toast.makeText(ChangeDeviceNameActivity.this, R.string.device_name_changed_failure, Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void successChange(String name) {
        Toast.makeText(ChangeDeviceNameActivity.this,R.string.device_name_changed_success,Toast.LENGTH_SHORT).show();
        Intent  intent = new Intent();
        intent.putExtra(DeviceInfoActivity.EXT_DEVICE_NAME,name);
        setResult(RESULT_OK,intent);
    }
}
